<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_tkel extends CI_Controller {
	function C_tkel(){
		parent::__construct();
		if($this->auth->is_logged_in() == false){
			redirect('c=c_login','refresh');
		}
		$this->load->model('m_tkel', '', TRUE);
	}
	
	public function index(){
		$this->load->view('main/v_tkel');
	}
	
	function get_list(){
		$result=$this->m_tkel->get_list();
		echo $result;
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "CREATE":
				$this->create();
				break;
			case "UPDATE":
				$this->update();
				break;
			case "DELETE":
				$this->delete();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	function create(){
		$validto=trim(@$_POST["validto"]);
		$validto=htmlentities($validto,ENT_QUOTES);
		$nourut=trim(@$_POST["nourut"]);
		$nourut=htmlentities($nourut,ENT_QUOTES);
		$grade=trim(@$_POST["grade"]);
		$grade=htmlentities($grade,ENT_QUOTES);
		$kodejab=trim(@$_POST["kodejab"]);
		$kodejab=htmlentities($kodejab,ENT_QUOTES);
		$nik=trim(@$_POST["nik"]);
		$nik=htmlentities($nik,ENT_QUOTES);
		$rptistri=trim(@$_POST["rptistri"]);
		$rptistri=htmlentities($rptistri,ENT_QUOTES);
		$rptanak=trim(@$_POST["rptanak"]);
		$rptanak=htmlentities($rptanak,ENT_QUOTES);
		$username=trim(@$_POST["username"]);
		$username=htmlentities($username,ENT_QUOTES);
		
		$result=$this->m_tkel->create($validto,$nourut,$grade,$kodejab,$nik,$rptistri,$rptanak,$username);
		echo $result;
	}
	
	function update(){
		$validto=trim(@$_POST["validto"]);
		$validto=htmlentities($validto,ENT_QUOTES);
		$nourut=trim(@$_POST["nourut"]);
		$nourut=htmlentities($nourut,ENT_QUOTES);
		$grade=trim(@$_POST["grade"]);
		$grade=htmlentities($grade,ENT_QUOTES);
		$kodejab=trim(@$_POST["kodejab"]);
		$kodejab=htmlentities($kodejab,ENT_QUOTES);
		$nik=trim(@$_POST["nik"]);
		$nik=htmlentities($nik,ENT_QUOTES);
		$rptistri=trim(@$_POST["rptistri"]);
		$rptistri=htmlentities($rptistri,ENT_QUOTES);
		$rptanak=trim(@$_POST["rptanak"]);
		$rptanak=htmlentities($rptanak,ENT_QUOTES);
		$username=trim(@$_POST["username"]);
		$username=htmlentities($username,ENT_QUOTES);
		
		$result=$this->m_tkel->update($validto,$nourut,$grade,$kodejab,$nik,$rptistri,$rptanak,$username);
		echo $result;
	}
	
	function delete(){
		$grade=trim(@$_POST["grade"]);
		$grade=htmlentities($grade,ENT_QUOTES);
		
		$result=$this->m_tkel->delete($grade);
		echo $result;
	}
	
	function export_excel(){
		$rows=@$_POST["rows"];
		$this->firephp->log($rows);
		foreach($rows[0] as $field=>$value){
			$this->firephp->log($field);
		}
		$this->load->library('excel');//Panggil Library Excel
		
		$this->excel->setActiveSheetIndex(0);
		
		//set column name
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, 'VALIDTO');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(1, 1, 'NOURUT');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(2, 1, 'GRADE');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(3, 1, 'KODEJAB');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(4, 1, 'NIK');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(5, 1, 'RPTISTRI');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(6, 1, 'RPTANAK');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(7, 1, 'USERNAME');
		
		//set records
		for($i=0; $i<sizeof($rows); $i++){
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $rows[$i]['VALIDTO']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $rows[$i]['NOURUT']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $rows[$i]['GRADE']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $rows[$i]['KODEJAB']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $rows[$i]['NIK']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $rows[$i]['RPTISTRI']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $rows[$i]['RPTANAK']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $rows[$i]['USERNAME']);
		}
		$this->excel->getActiveSheet()->setTitle('tkel_list');
		
		$this->excel->setActiveSheetIndex(0);
		
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
		
		$objWriter->save("./documents/tkel.xlsx");    //Simpan sebagai apalah.xlsx*/
		
		echo 1;
	}
	
	function export_pdf(){
		$rows = isset($_POST['rows']) ? @$_POST['rows'] : @$_GET['rows'];
		/*$this->load->library('dom_pdf');
		$rows = isset($_POST['rows']) ? @$_POST['rows'] : @$_GET['rows'];
		$this->firephp->log($rows);
		
		$data["rows"] = $rows;
		
		$content = "main/p_grade";
		$filename = "grade";
		$orientation = "potrait";
		
		$html = $this->load->view($content,$data,TRUE);//Save as variable
		$this->dom_pdf->pdf_create($html, $filename);*/
		
		$this->load->library('cezpdf');
		$this->load->helper('pdf');
		prep_pdf();
		
		if(sizeof($rows) > 0){
			for($i=0; $i<sizeof($rows); $i++){
				$db_data['tkeluarga'] = array(
						'validto' => $rows[$i]['VALIDTO'], 
						'nourut' => $rows[$i]['NOURUT'], 
						'grade' => $rows[$i]['GRADE'], 
						'kodejab' => $rows[$i]['KODEJAB'], 
						'nik' => $rows[$i]['NIK'], 
						'rptistri' => $rows[$i]['RPTISTRI'], 
						'rptanak' => $rows[$i]['RPTANAK'],
						'keterangan' => $rows[$i]['USERNAME']);
			}
		}else{
			$db_data['tkeluarga'] = array(
						'validto' => '', 
						'nourut' => '', 
						'grade' => '', 
						'kodejab' => '', 
						'nik' => '', 
						'rptistri' => '', 
						'rptanak' => '',
						'keterangan' => '');
		}
		
		$col_names = array(
			'validto' => 'VALIDTO', 
			'nourut' => 'NOURUT', 
			'grade' => 'GRADE', 
			'kodejab' => 'KODEJAB', 
			'nik' => 'NIK', 
			'rptistri' => 'RPTISTRI', 
			'rptanak' => 'RPTANAK',
			'keterangan' => 'USERNAME'
			);
		
		$this->cezpdf->ezTable($db_data, $col_names, 'List Tunjangan Keluarga', array('width'=>550));
		$this->cezpdf->ezStream();
	}
	
	function cetak(){
  		$rows = isset($_POST['rows']) ? @$_POST['rows'] : @$_GET['rows'];
		
		$data["rows"] = $rows;
		
		$print_view=$this->load->view("main/p_tkel.php",$data,TRUE);
		if(!file_exists("documents")){
			mkdir("documents");
		}
		$print_file=fopen("documents/tkellist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */